package com.analysis.yejin.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import com.analysis.yejin.data.CodeDataModel;
import com.analysis.yejin.data.CodeHierarchy;


public class AnalysisEditor extends EditorPart {

	public static final String ID = "com.analysis.yejin.editor1";
	private AnalysisInput input;
	private CodeDataModel model;
	private GC gc;
	
	public AnalysisEditor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		// TODO Auto-generated method stub
		if (!(input instanceof AnalysisInput)) {
		      throw new RuntimeException("Wrong input");
		    }

		AnalysisInput new_name = (AnalysisInput) input;
		this.input = (AnalysisInput) input;
		setSite(site);
		setInput(input);
		
		this.model = CodeDataModel.getInstance();
		
		setPartName("Analyze result");
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		//GridLayout layout = new GridLayout();
	    //layout.numColumns = 4;
	    //parent.setLayout(layout);
	    
	    //ScrollBar bar1 = parent.getVerticalBar();
	    
		ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
	    Canvas canvas = new Canvas(sc, SWT.NONE);
	    
	    
	    sc.setBackground(getSite().getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
	    canvas.setBackground(getSite().getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
	    canvas.setBounds(0, 0, 10000, 10000);
	    canvas.redraw();
	    
	    
	    
	    
	    canvas.addPaintListener(new PaintListener(){

			@Override
			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				drawExample(e.gc);
			}
	    	
	    });
	    canvas.redraw();
	    sc.setContent(canvas);
	    /*
	    Label label1 = new Label(parent, SWT.NONE);
	    label1.setText("1 data");
	    Text dataTest1 = new Text(parent, SWT.BORDER);
	    dataTest1.setText(model.getCodeList().get(0).getName());
	    dataTest1.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
	    
	    
	    Label label2 = new Label(parent, SWT.NONE);
	    label2.setText("2 data");
	    Text dataTest2 = new Text(parent, SWT.BORDER);
	    dataTest2.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
	    dataTest2.setText(model.getCodeList().get(1).getName());
	    
	    Label label3 = new Label(parent, SWT.NONE);
	    label3.setText("3 data");
	    Text dataTest3 = new Text(parent, SWT.BORDER);
	    dataTest3.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
	    dataTest3.setText(model.getCodeList().get(2).getName());
	    
	    
	    Label label4 = new Label(parent, SWT.NONE);
	    label4.setText("4 data");
	    Text dataTest4 = new Text(parent, SWT.BORDER);
	    dataTest4.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
	    dataTest4.setText(model.getCodeList().get(3).getName());
	    */

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	private void drawExample(GC gc){
		Rectangle region1 = new Rectangle(10, 10, 300, 800);
		Rectangle region2 = new Rectangle(310, 10, 300, 800);
		Rectangle region3 = new Rectangle(610, 10, 300, 800);
		
		gc.drawText("r1", 130, 20);
		gc.drawText("r2", 450, 20);
		gc.drawText("r3", 800, 20);
		
		
		gc.drawRectangle(region1);
		gc.drawRectangle(region2);
		gc.drawRectangle(region3);
		
		gc.drawRectangle(100, 150, 150, 100);
		gc.drawText("val2:example.Calculate", 120, 190);
		
		
		gc.drawRectangle(330, 50, 200, 120);
		gc.drawText("setValue():example.Calculate", 340, 80);
		gc.drawRectangle(330, 190, 200, 120);
		gc.drawText("sum():example.Calculate", 340, 220);
		gc.drawRectangle(330, 330, 200, 120);
		gc.drawText("subtraction():example.Calculate", 340, 360);
		gc.drawRectangle(330, 470, 200, 120);
		gc.drawText("multiplication():example.Calculate", 340, 500);
		gc.drawRectangle(330, 610, 200, 120);
		gc.drawText("division():example.Calculate", 340, 640);
		
		
		gc.drawRectangle(650, 130, 160, 100);
		gc.drawText("print():example.Calculate", 660, 190);
		
		
		Rectangle hidden = new Rectangle(100, 830, 150, 100);
		gc.setBackground(getSite().getShell().getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
		gc.fillRectangle(hidden);
		gc.drawText("val1:example.Calculate", 120, 840);
		gc.setBackground(getSite().getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
		
		
		
		
	}

}
